Welcome to Request and Response Objects. After watching this video, you will be able to Explain the Flask Request Object and Explain the Flask Response Object. Let's learn how to customize routes in Flask. You define the path using the RouteDecorator, and the AddApp.RouteDecorator defaults to the Get method. Clients can only send Get requests to the specified path. Now, you can pass in a second argument of methods to control what HTTP methods the path responds to. For example, look at the following two methods that are the same. The Get method is implicit in this code, while this code explicitly specifies the Get method. Here's another example. The slash health URL will respond to Get and Post requests. If the request is Get, the code outputs the status of OK and the method of Get. If the request is Post, the code outputs the status of OK and the method of Post. This is the output from the Get curl command, while this is the output from the Post curl command. All HTTP calls to Flask contain the request object created from the Flask.Request class. When a client requests a resource from the Flask server, it is handled by the AddApp.RouteDecorator. You can examine and explore the request object in the same method. Now, the following information is available in the request object. The address of the server in the form of a tuple host port, the headers sent with the request, and the URL that is the resource asked by the request. AccessRoute that lists all the IP addresses for requests that are forwarded multiple times. FullPath that represents the complete path of the request, including any query string. IsSecure is true if a client makes a request using HTTPS or WSS protocol. IsJSON is true if the request contains JSON data. And the Cookies dictionary contains any cookies sent with the request. In addition, you can access the following data from the header. CacheControl holds information on how to cache in browsers. Accept informs the browser what kind of content type is understood by the client. AcceptEncoding indicates the code content. UserAgent identifies the client, application, operating system, or version. AcceptLanguage requests for a specific language and locale. And Host specifies the host and port number of the requested server. Now, you can replace the request object with a custom request object, which is usually optional as the Flask request class attributes and methods are enough. Let's now look at some actual values printed on the server when a client makes a request. In this case, the client is the curl command from the terminal. The request server is 127.0.0.1, or localhost at port 5000. You next print out some headers. The host is the full path of localhost colon 5000. The user agent is curl version 7.79.1. And the client has requested the content type of application slash JSON. Okay, let's look at some more attributes from the request object. The URL requested is http colon forward slash forward slash localhost colon 5000. The access route list contains the single value of 127.0.0.1. The full path for the request is the home path indicated by a single forward slash. Is JSON is false as we are not sending any data with the GET request. Is secure is false as the URL is HTTP and not HTTPS. And the length of the cookies dictionary is zero. There are multiple ways to get information from the request object. Use the GET data to access data from the POST request as bytes. You are then responsible for parsing this data. You can also use the GET JSON method to get data parsed as JSON from the POST request. Now, Flask also provides more focused methods to get exact information from the request so you don't have to parse the data into a specific type. Args will give you query parameters as a dictionary. JSON will parse the data into a dictionary. Files will provide your user uploaded files. Form contains all values posted in a form submission. And Values combines the Args data with the Form data. Now that you know what the request object looks like and the methods to get data from the request parameters and body, let's see how to extract specific values from this data. The return types for the methods you have seen so far are either a Multidict, Immutable Multidict, or a Combined Multidict. All these data structures behave like a Python dictionary and you can use indexing or the GET method to extract values. For the given URL, you would like to extract the query parameters of Capstone and Rating. First, you import Flask and the request modules in your method. You extract the value for the course argument by using indexing. Then, you extract the value for the rating argument by using the GET method. The GET method returns a value of None if the argument is not present, whereas the indexing method returns an error and stops the server with a 400 bad request. The message shows the request in the browser with a course of Capstone and a rating of 10. Flask will provide a response object just like it provides a request object, and you can use the response object to send custom attributes and headers back to the client. Some common response attributes include the following. Status code indicates the success or failure of the request. The headers give more information about the response. Content type shows the media type of the requested resource. Content length is the size of the response message body. Content encoding indicates any encoding applied to the response so the client knows how to decode the data. MIME type sets the media type of the response. And expires contains the date or time after which the response is considered expired. Here are some standard methods on response objects. Set cookie sets a browser cookie on the client. And delete cookie deletes a cookie on the client. Let's now learn how Flask works with the response object using the different methods. A response object with the status code of 200 and a MIME type of HTML is automatically created for you when you return data from the atRoute method. JSONify also creates a response object automatically. You can use makeResponse to create a custom response. Flask provides a special redirect method to return a 302 status code and redirect the client to another URL. And finally, Flask provides an abort method to return a response with an error condition. In this video you learned that Flask provides a request and a response object for each client call. You can get additional information from the Flask request like headers. You can parse the request object to get query parameters, body, and other arguments. And you can set status on response objects before sending a response back to the client.